// pages/record/index.js
import {
  formatTime
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: ['', '', '', '', '', '', '', ''],
    minDate: new Date(2021, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    numIndex: 0,
    headTop: getApp().globalData.headTop,
    show1: false,
    // 所有街道
    allParkingLotList: [],
    parkingName: '',
    show2: false,
    allParkingSpace: [],
    parkingSpaceName: '',
    show3: false,
    params: {
      IllegalLicensePlate: '',
      parkingLotId: '',
      parkingSpaceId: '',
      tollCollectorId: '',
      params: {
        recordStartTime: '',
        recordEndTime: ''
      },
      list:[]
    }
  },
  // 返回首页
  onClickLeft() {
    wx.switchTab({
      url: '/pages/work/index',
    })
  },
  // 查询违章记录
  getEvidenceRecordList() {
    const params = this.data.params
    this.setData({
      list: [{
          illegalParkingEvidenceEecordId: 1,
          IllegalLicensePlate: '苏e12345',
          parkingSpaceName: '承平街001车位',
          recordTime: '2023.22.09 09: 00: 00'
        },

        {
          illegalParkingEvidenceEecordId: 2,
          IllegalLicensePlate: '苏e12445',
          parkingSpaceName: '承平街002车位',
          recordTime: '2023.22.09 09: 00: 00'
        },
      ]
    })
  },
  getallParkingLotList() {
    // 模拟接口查询街道
    this.setData({
      allParkingLotList: [{
        parkingLotId: 100,
        parkingName: '测试停车场'
      }]
    })
  },
  // 选择某街道
  onConfirm(e) {
    console.log(e)
    this.onClose()
    if (Array.isArray(e.detail)) {
      // 日期选择
      this.setData({
        "params.params.recordStartTime": formatTime(e.detail[0]),
        "params.params.recordEndTime": formatTime(e.detail[1]),
      })
    } else if (e.detail.value.parkingLotId) {
      // 选择街道
      this.setData({
        "params.parkingLotId": e.detail.value.parkingLotId,
        parkingName: e.detail.value.parkingName,
        "params.parkingSpaceId": '',
        parkingSpaceName: ''
      })
      this.getallParkingSpace(e.detail.value.parkingLotId)
    } else if (e.detail.value.parkingSpaceId) {
      // 选择车位
      this.setData({
        "params.parkingSpaceId": e.detail.value.parkingSpaceId,
        parkingSpaceName: e.detail.value.parkingSpaceName
      })
    }

  },
  getallParkingSpace(parkingLotId) {
    // 查询某停车场的所有车位
    let params = {
      parkingLotId
    }
    this.setData({
      allParkingSpace: [{
        parkingSpaceId: 100,
        parkingSpaceName: '测试001车位'
      }]

    })
  },
  // 显示选择街道
  showPopup(e) {
    console.log(e)
    const {
      index
    } = e.currentTarget.dataset
    if (index == 2 && this.data.parkingName == '') {
      wx.showToast({
        title: '请先选择街道',
        icon: 'none'
      })
      return
    }
    this.setData({
      [`show${index}`]: true
    });
  },
  // 关闭弹窗
  onClose() {
    this.setData({
      show1: false,
      show2: false,
      show3: false
    });
  },
  // 输入框改变事件
  numInput(e) {
    const {
      indx
    } = e.currentTarget.dataset
    const {
      value
    } = e.detail
    this.setData({
      ['num[' + indx + ']']: value,
      numIndex: (indx * 1) + 1
    })
    // 输入最后一位数校验
    if (indx == 7) {
      this.setData({
        'params.IllegalLicensePlate': this.data.num.join('')
      })
    }
  },
  // 返回首页
  onClickLeft() {
    wx.switchTab({
      url: '/pages/work/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getallParkingLotList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        list: [{
            "pagePath": "/pages/record/index",
            "text": "违章记录",
            "iconPath": "/images/tabbar/record.png",
            "selectedIconPath": "/images/tabbar/recorda.png"
          },
          {
            "pagePath": "/pages/evidence/index",
            "text": "违章取证",
            "iconPath": "/images/tabbar/evidence.png",
            "selectedIconPath": "/images/tabbar/evidencea.png"
          }
        ]
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})